$(function() {
	
	/**
	 *  进入界面查询数据库
	 *  
	 */
	gw();

	 
	/**
	 *  刷新界面查询购物车
	 */
   function gw(){
		var tt = $(".tt1");

		let t = tt.children().remove();
			
		$.get("gwcsel.do", function(data) {
		 
			 
			$.each(data, function(i, item) {
		
				//总价
			let  pr=  Number(item.foodCarPriceCount);
			//单价
			let  pr1=  Number(item.foodCarPrice);
			let  hideprice="<input type=\"hidden\" value=\""+pr1+"\" disabled=\"disabled\" class=\"price\" style=\"width: 60px;text-align: center;\"/>"
		    let  name="<span class=\"text\">"+item.foodCarName+"</span>"
			let  aa="<input type=\"text\" name=\"yin\" value=\""+pr+"\" disabled=\"disabled\" class=\"price\" style=\"width: 60px;text-align: center;\"/>"
            let  bb = "<a  href=\"javascript:void(0)\" class=\"dianwo-\"> <input type=\"button\" style=\"width: 50px\" value=\"-\" class=\"btn btn-success\" /></a>"
            let  cc="<input type=\"text\" value=\""+item.foodCarNumber+"\" name=\"count\" style=\"width: 50px;text-align: center;\" class=\"zhi\" />";
			let  dd="<a href=\"javascript:void(0)\" class=\"dianwo\" ><input type=\"button\" style=\"width: 50px\" value=\"+\" class=\"jia btn btn-success\" />"				
			let  ee="<a href=\"javascript:void(0)\" class=\"del\" ><input type=\"button\" style=\"width: 50px\" value=\"x\" class=\"btn btn-success\" /></a>"
			let  ff="<input type=\"hidden\" style=\"width: 50px\" value=\""+item.foodCarId+"\" class=\"rr\" />"
			let  ttd = "<td class=\"num\">" +hideprice+name+aa+bb+cc+dd+ee+ff+ "</td>"
	
		    tt.append("<tr>" + ttd+ "</tr>")

			}) 

		}, "json"

	)
	}
	
	
	
	
/*	$("body").on("erroy","img",function(){
		alert("asd")
		 $(this).attr("src","../../img/_noimg.png")
	})*/
  /* $("img").error(function(){
	       alert("Asd")
	 });*/
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 *  将数据加入购物车
	 */
	
	
	
	
	$(".cd").on("click", function() {

		let href = $(this).attr("data-type")
		var tt = $(".tt1");

		let t = tt.children().remove();
		$.get("gwc.do", {
				id: href
			}, function(data) {
			 
				
				$.each(data, function(i, item) {
					//总价
				let  pr=  Number(item.foodCarPriceCount);
				//单价
				let  pr1=  Number(item.foodCarPrice);
				let  hideprice="<input type=\"hidden\" value=\""+pr1+"\" disabled=\"disabled\" class=\"price\" style=\"width: 60px;text-align: center;\"/>"
			    let  name="<span class=\"text\">"+item.foodCarName+"</span>"
				let  aa="<input type=\"text\" name=\"yin\" value=\""+pr+"\" disabled=\"disabled\" class=\"price\" style=\"width: 60px;text-align: center;\"/>"
                let  bb = "<a  href=\"javascript:void(0)\" class=\"dianwo-\"> <input type=\"button\" style=\"width: 50px\" value=\"-\" class=\"btn btn-success\" /></a>"
                let  cc="<input type=\"text\" value=\"1\" name=\"count\" style=\"width: 50px;text-align: center;\" class=\"zhi\" />";
				let  dd="<a href=\"javascript:void(0)\" class=\"dianwo\" ><input type=\"button\" style=\"width: 50px\" value=\"+\" class=\"jia btn btn-success\" />"				
				let  ee="<a href=\"javascript:void(0)\" class=\"del\" ><input type=\"button\" style=\"width: 50px\" value=\"x\" class=\"btn btn-success\" /></a>"
				let  ff="<input type=\"hidden\" style=\"width: 50px\" value=\""+item.foodCarId+"\" class=\"rr\" />"
				let  ttd = "<td class=\"num\">" +hideprice+name+aa+bb+cc+dd+ee+ff+ "</td>"
		
			    tt.append("<tr>" + ttd+ "</tr>")
  
				})
  
			}, "json"
 
		)
	})


	
		//  计算增加的商品和数量 
		  $("body").on("click",".dianwo",function(){
		           
		          //拿到数量
			      let count=  $(this).prev().val()
		        
		           let nu= Number(count);  
			      //设置值  
	              $(this).prev().attr("value", nu+1 )
	              
	          let number=   Number(  $(this).prev().attr("value"));
		           //获取设置价格	      
		           
		         //获取初始价格
		       let price=  Number( $(this).prev().prev().prev().prev().prev().attr("value"))
		   
		        
		       let  pricesum=(number*price).toFixed(2);
		       
		       //设置参数
		         $(this).prev().prev().prev().attr("value",pricesum)
		       //    alert(price)
		         
		         //获取id
		      let  id=    Number(  $(this).next().next().attr("value"))
		       
		      //更改购物车数据
		      $.get("pay.do", {count: number,pricesum:pricesum,id:id }, function() {
			
			}
			   
		   )
			
		         
		         
		         
		       
		       
		        })
		        
		    	
				//  计算减少的商品和数量 
				  $("body").on("click",".dianwo-",function(){
				           
				          //拿到数量
					      let count=  $(this).next().val()
				        
				           let nu= Number(count);  
					      if(nu<=1){
					    	  
					    	  nu=1;
					    	return false;
					      }else{
					    	   //设置值  
				              $(this).next().attr("value", nu-1 )
					      }
					    let nu1=  $(this).next().attr("value" )
					    
			     
				       //获取当前价格
				        let price=  Number( $(this).prev().attr("value"))
				         //获取初始价格
				        let  pricee1=   Number( $(this).prev().prev().prev().attr("value"));
				        //计算
				        let  pricesum=(price-pricee1).toFixed(2);
				          
				       //设置参数
				         $(this).prev().attr("value",pricesum)
				     
				         //获取id
					      let  id=Number(  $(this).next().next().next().next().attr("value"))
					  
					      //更改购物车数据
					      $.get("pay.do", {count: nu1,pricesum:pricesum,id:id }, function() {
							
						}
						   
					   )
						
				         
				         
				        })
				        				        				        				        				        
				   // 删除购物车数据
				  $("body").on("click",".del",function(){
			
					  let id= Number($(this).next().val());
						$(this).parent().remove();
				
				    $.get("del.do", {id:id }, function(data) {
						// alert(data.ok)
					})
				  })		
				    
				  
				  
				  //立即购买
				  	  $("body").on("click",".lz",function(){
				  		  
				  		let href = $(this).attr("data-type")
				  		 $.get("insert.do",{id:href},function(data){
				  		
				  			location.href="pay1.do";		  			 				  			 
				  		 }				  				 
				  		  
				  		 ) 		  
				  	  })
				  
				  
				  
				  
				  
				  //将购物车数据添加到结算界面
				    $("body").on("click",".tj",function(){
				    	
				    	let arr = [];				    
				    	$(".num").each(function(i,v){ 
				    		arr[i] = {foodCarName:$(this).find(".text").text(),foodCarNumber:$(this).find("input[name='count']").val(), foodCarPrice:$(this).find("input[name='yin']").val()};
				    	
				    		   console.info(JSON.stringify(arr));
				    	}); 
				    
			
				    	 var data = arr; 
				    
				    	 $.ajax({ 
				    		    type:"post",
				    		    url: "pay.do", 
				    		    data:JSON.stringify(arr), 
				    		    dataType: "json", 
				    		    contentType:"application/json",//设置类型传入后台
				    		    success: function () { 
				    		         //	alert("ok")
				    		         	location.href="pay1.do";
				    		    } 
				    		  });  
				    		
				    	
				    	
				    })
				  
				  
				  
	

})